package com.codename1.samples;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import com.codename1.io.Util;
import com.codename1.ui.Button;
import com.codename1.ui.Component;
import com.codename1.ui.Container;
import com.codename1.ui.Graphics;
import com.codename1.ui.Image;
import com.codename1.ui.geom.Dimension;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.GridLayout;
import com.codename1.ui.layouts.Layout;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class ZoomOutImageSample {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }
    
    private ImageCmp lbl;
    
    /**
     * Simple component that renders an image.
     */
    static class ImageCmp extends Component {
        private Image img;
        private double scale = 1.0;

        /**
         * Create image component to render given image.
         * @param img Image to render
         */
        ImageCmp(Image img) {
            this.img = img;
        }
        
        @Override
        protected Dimension calcPreferredSize() {
            return new Dimension((int)(img.getWidth()*scale), (int)(img.getHeight()*scale));
        }
        
        /**
         * Set the scale of the image.  This affects preferred size.
         * @param scale 
         */
        void setScale(double scale) {
            
            this.scale = scale;
            setShouldCalcPreferredSize(true);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.drawImage(img, getX(), getY(), getWidth(), getHeight());
        }
        
        
        
    }
    
    static class ZoomLayout extends Layout {

        @Override
        public void layoutContainer(Container cnt) {
            for (Component cmp : cnt) {
                cmp.setX((cnt.getLayoutWidth() - cmp.getPreferredW())/2);
                cmp.setY((cnt.getLayoutHeight() - cmp.getPreferredH())/2);
                cmp.setWidth(cmp.getPreferredW());
                cmp.setHeight(cmp.getPreferredH());
            }
        }

        @Override
        public Dimension getPreferredSize(Container cnt) {
            Dimension dim = new Dimension();
            for (Component cmp : cnt) {
                dim.setWidth(Math.max(cmp.getPreferredW(), dim.getWidth()));
                dim.setHeight(Math.max(cmp.getPreferredH(), dim.getHeight()));
                
            }
            return dim;
        }
        
    }
    
    public void start() {
        if(current != null){
            current.show();
            return;
        }
        // NOTE: We use CENTER_BEHAVOUR_CENTER_ABSOLUTE so that the center image
        // will take on its preferred size.  THis allows the ImageCmp to specify its
        // size.
        Form hi = new Form("Hi World", new BorderLayout());
        Button zoomIn = new Button("Zoom In");
        zoomIn.addActionListener(evt->{
            if (lbl == null) {
                return;
            }
            lbl.setScale(10.0);
            
            // Zoom in over 1 second
            lbl.getParent().animateLayout(1000);
        });
        Button zoomOut = new Button("Zoom Out");
        zoomOut.addActionListener(evt->{
            if (lbl == null) {
                return;
            }
            lbl.setScale(0.1);
            
            // Zoom out over 1 second
            lbl.getParent().animateLayout(1000);
        });
        
        // Download an image... then add it to the form.
        Util.downloadImageToStorage("https://www.codenameone.com/img/uber-skin.png", "uber-skin.png").ready(img->{
            lbl = new ImageCmp(img);
            Container cnt = new Container(new ZoomLayout());
            cnt.add(lbl);
            hi.add(CENTER, cnt);
            hi.revalidateWithAnimationSafety();
        });
        hi.add(NORTH, GridLayout.encloseIn(2, zoomIn, zoomOut));
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }
    
    public void destroy() {
    }

}
